package com.smec.moblie.activity;

import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smec.moblie.R;
import com.smec.moblie.adapter.FastrepairHistoryAdapter;
import com.smec.moblie.bean.MaintenanceFastrepairhisBean;
import com.smec.moblie.crash.ExitAppUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;

/**
 * 急修历史列表
 * <p/>
 * Created by sunyubo on 15/12/22.
 */
public class FastrepairHistoryListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    /**
     * 急修历史列表
     **/
    private ListView listView_fastrepairHis;
    /**
     * 返回按钮
     **/
    private ImageView imageview_back;
    /**
     * 没有数据时显示文字
     **/
    private TextView textView_noData;

    /**
     * 急修历史adapter
     **/
    private FastrepairHistoryAdapter fastrepairHistoryAdapter;

    /**
     * 急修历史list
     **/
    private ArrayList<MaintenanceFastrepairhisBean> maintenanceFastrepairhisBeanList;
    /** 电梯id **/
    private String ele_id;
    /** 下拉刷新 **/
    private SwipeRefreshLayout refreshLayout;

    private RelativeLayout rl_title;

    @Override
    protected void setContentView() {
        setContentView(R.layout.remes_act_fastrepairhistory);
    }

    @Override
    protected void initView() {
        ExitAppUtils.getInstance().addActivity(this);

        rl_title = (RelativeLayout)findViewById(R.id.rl_title);
//        PublicControl.setMargins(rl_title, 0, getStatusBarHeight(FastrepairHistoryListActivity.this), 0, 0);

        listView_fastrepairHis = (ListView) findViewById(R.id.listView_fastrepairHis);
        imageview_back = (ImageView) findViewById(R.id.imageview_back);
        imageview_back.setOnClickListener(this);

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Color.GREEN, Color.BLUE, Color.YELLOW);

        textView_noData = (TextView) findViewById(R.id.textView_noData);

        fastrepairHistoryAdapter = new FastrepairHistoryAdapter(FastrepairHistoryListActivity.this);
        listView_fastrepairHis.setAdapter(fastrepairHistoryAdapter);
    }

    @Override
    protected void getData() {

        ele_id = getIntent().getStringExtra("ele_id");

        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
            }
        });

        new GetFastrepairHisAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageview_back:
                FastrepairHistoryListActivity.this.finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
            }
        });

        new GetFastrepairHisAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    private class GetFastrepairHisAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            maintenanceFastrepairhisBeanList = (ArrayList<MaintenanceFastrepairhisBean>) DataSupport.where("ele_id = ?", ele_id).find(MaintenanceFastrepairhisBean.class);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            fastrepairHistoryAdapter.setList(maintenanceFastrepairhisBeanList);
            refreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    refreshLayout.setRefreshing(false);
                }
            });

            if (maintenanceFastrepairhisBeanList.size() > 0) {
                textView_noData.setVisibility(View.GONE);
            } else {
                textView_noData.setVisibility(View.VISIBLE);
            }
            super.onPostExecute(aVoid);
        }
    }
}
